package com.shs.com.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shs.com.entity.Book;
import com.shs.com.entity.BookSub;
import com.shs.com.mapper.BookMapper;
import com.shs.com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public Map<String, Object> queryBookList(Map<String, Object> map) {
        //开启分页
        PageHelper.startPage((Integer) map.get("pageNum"),(Integer) map.get("pageSize"));
               //查询
        List<Book> bookList = bookMapper.queryBookList(map);
        PageInfo<Book> pageInfo = new PageInfo<>(bookList);
        //获取总记录数
        long total = pageInfo.getTotal();
        //获取总页数据
        List<Book> pageInfoList = pageInfo.getList();
//        把查询结果放到一个Map
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", total);
        resultMap.put("rows", pageInfoList);
        return resultMap;
    }

    //添加图书
    @Override
    public void addBook(Map<String, Object> map) {
        bookMapper.addBook(map);
    }

    //编辑图书
    @Override
    public void editBook(Map<String, Object> map) {
        bookMapper.editBook(map);
    }

//    删除书籍
    @Override
    public void delBook(Map<String, Object> map) {
        bookMapper.delBook(map);
    }

//    跟新库存
    @Override
    public void updateInventtories(Map<String, Object> map) {
     bookMapper.updateInventtories(map);
    }

//    根据id查询图书
    @Override
    public Book getBookById(int id) {
        return bookMapper.getBookById(id);
    }

    //查看借阅图书列表
    @Override
    public List<BookSub> querySub(Map<String, Object> map) {
        return bookMapper.querySub(map);
    }
    //借阅图书
    @Override
    public void addSubBook(Map<String, Object> map) {

        bookMapper.addSubBook(map);
    }
    //归还图书
    @Override
    public void returnSubBook(Map<String, Object> map) {

        bookMapper.returnSubBook(map);
    }

}
